package com.ssmTest.controller;


import com.ssmTest.model.MachineUserInformation;
import com.ssmTest.model.UserInformation;
import com.ssmTest.service.IMachineUserInformationService;
import com.ssmTest.service.IUserInformationService;
import com.ssmTest.service.IUserService;
import com.ssmTest.service.impl.DownLoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.List;

@Controller
public class InformationController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IMachineUserInformationService iMachineUserInformationService;
    @Autowired
    private IUserInformationService iUserInformationService;
    @Autowired
    private DownLoadService downLoadService;

    //普通用户信息审核界面
    @RequestMapping(value = "/userInformationAudit.do")
    public String userInformationAudit(ModelMap modelMap){
        List<UserInformation> list=null;
        int i = 0;
        list=iUserInformationService.InformationList(i);
        modelMap.addAttribute("list",list);
        return "informationByUser";
    }
    //农机手用户信息审核界面
    @RequestMapping(value = "/machineuserInformationAudit.do")
    public String machineuserInformationAudit(ModelMap modelMap){
        List<MachineUserInformation> list=null;
        int i = 0;
        list=iMachineUserInformationService.MachineInformationList(i);
        modelMap.addAttribute("list",list);
        return "informationByMachineUser";
    }

    //普通用户审核成功
    @RequestMapping("/auditInformationSuccess.do")
    public String auditInformationSuccess(HttpSession session, int id,ModelMap modelMap) {
        UserInformation userInformation=iUserInformationService.selectUserInformation(id);
        userInformation.setToexamine(1);
        iUserInformationService.updateInformationReasion(userInformation);
        List<UserInformation> list = null;
        list=iUserInformationService.InformationList(0);
        modelMap.addAttribute("list",list);
        return "informationByUser";
    }

    //机手审核成功
    @RequestMapping("/auditmachineInformationSuccess.do")
    public String auditmachineInformationSuccess(HttpSession session, int id,ModelMap modelMap) {
        MachineUserInformation machineUserInformation=iMachineUserInformationService.selectMachineUserInformation(id);
        machineUserInformation.setToexamine(1);
        iMachineUserInformationService.updateInformationReasion(machineUserInformation);
        List<MachineUserInformation> list = null;
        list=iMachineUserInformationService.MachineInformationList(0);
        modelMap.addAttribute("list",list);
        return "informationByMachineUser";
    }

    //普通用户弹出界面
    @RequestMapping("/writeinformationReasionPage.do")
    public  String writeReasionPage(ModelMap modelMap,int id,HttpSession session){
        session.setAttribute("mid",id);
        modelMap.addAttribute("id",id);
        modelMap.addAttribute("url","informationauditFail.do");
        return "writeReasionPage";
    }

    //机手用户弹出界面
    @RequestMapping("/writemachineinformationReasionPage.do")
    public  String writemachineinformationReasionPage(ModelMap modelMap,int id,HttpSession session){
        session.setAttribute("mid",id);
        modelMap.addAttribute("id",id);
        modelMap.addAttribute("url","machineinformationauditFail.do");
        return "writeReasionPage";
    }

    //机手信息审核失败
    @RequestMapping("/machineinformationauditFail.do")
    public String machineinformationauditFail(HttpSession session, String reasion, ModelMap modelMap) {
        MachineUserInformation machineUserInformation=iMachineUserInformationService.selectMachineUserInformation(Integer.parseInt(session.getAttribute("mid").toString()));
        machineUserInformation.setFail_reasion(reasion);
        machineUserInformation.setToexamine(2);
        iMachineUserInformationService.updateInformationReasion(machineUserInformation);
        return "success";
    }

    //普通用户信息审核失败
    @RequestMapping("/informationauditFail.do")
    public String selectfailUser(HttpSession session, String reasion) {
        UserInformation userInformation=iUserInformationService.selectUserInformation(Integer.parseInt(session.getAttribute("mid").toString()));
        userInformation.setFail_reasion(reasion);
        userInformation.setToexamine(2);
        iUserInformationService.updateInformationReasion(userInformation);
        return "success";
    }

    //下载信息表
    @RequestMapping("/downloadOrderList.do")
    public String downloadOrderList() {
        return "downLoadOrder";
    }

    //下载信息表
    @RequestMapping("/downloadOrder.do")
    public String downloadOrder(HttpServletRequest request, HttpServletResponse response) throws ParseException {
        downLoadService.download(request,response);
        return "success";
    }
}
